package com.huadi.ssm.controller;



import com.huadi.ssm.page.Page;
import com.huadi.ssm.po.Organization;
import com.huadi.ssm.service.OrganizationService;
import com.huadi.ssm.util.StringUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 组织机构管理
 *
 */
@RequestMapping("/organization")
@Controller
public class OrganizationController {

    @Autowired
    private OrganizationService organizationService;

    /**
     * 组织机构列表页
     * @param model
     * @return
     */
    @RequestMapping(value="/list",method= RequestMethod.GET)
    public ModelAndView list(ModelAndView model){
        model.setViewName("organization/organization_list");
        return model;
    }

    /**
     * 获取组织机构列表
     * @param name
     * @param page
     * @return
     */
    @RequestMapping(value="/get_list",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getList(
            @RequestParam(value="name",required=false,defaultValue="") String name,
            Page page
    ){
        Map<String, Object> ret = new HashMap<String, Object>();
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("name", "%"+name+"%");
        queryMap.put("offset", page.getOffset());
        queryMap.put("pageSize", page.getRows());
        ret.put("rows", organizationService.findList(queryMap));
        ret.put("total", organizationService.getTotal(queryMap));
        return ret;
    }

    /**
     * 编辑组织机构信息
     * @param organization
     * @return
     */
    @RequestMapping(value="/edit",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> edit(Organization organization){
        Map<String, String> ret = new HashMap<String, String>();
        if(StringUtils.isEmpty(organization.getName())){
            ret.put("type", "error");
            ret.put("msg", "组织机构名称不能为空！");
            return ret;
        }
        if(organizationService.edit(organization) <= 0){
            ret.put("type", "error");
            ret.put("msg", "组织机构修改失败！");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "组织机构修改成功！");
        return ret;
    }


    /**
     * 添加组织机构信息
     * @param organization
     * @return
     */
    @RequestMapping(value="/add",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> add(Organization organization){
        Map<String, String> ret = new HashMap<String, String>();
        if(StringUtils.isEmpty(organization.getName())){
            ret.put("type", "error");
            ret.put("msg", "组织机构名称不能为空！");
            return ret;
        }
        if(organizationService.add(organization) <= 0){
            ret.put("type", "error");
            ret.put("msg", "组织机构添加失败！");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "组织机构添加成功！");
        return ret;
    }

    /**
     * 删除组织机构信息
     * @param ids
     * @return
     */
    @RequestMapping(value="/delete",method=RequestMethod.POST)
    @ResponseBody
    public Map<String, String> delete(
            @RequestParam(value="ids[]",required=true) Long[] ids
    ){
        Map<String, String> ret = new HashMap<String, String>();
        if(ids == null || ids.length == 0){
            ret.put("type", "error");
            ret.put("msg", "请选择要删除的数据！");
            return ret;
        }
        try {
            if(organizationService.delete(StringUtil.joinString(Arrays.asList(ids), ",")) <= 0){
                ret.put("type", "error");
                ret.put("msg", "删除失败！");
                return ret;
            }
        } catch (Exception e) {
            // TODO: handle exception
            ret.put("type", "error");
            ret.put("msg", "该组织机构下存在人员信息，请勿冲动！");
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "组织机构删除成功！");
        return ret;
    }

}
